#!/usr/bin/node
//计算器
const log = console.log,
      arg = process.argv[2];

/*try{
  log(`${argv}=${eval(argv)}`);
}catch(e){
  console.error('something wrong!');
}*/

//node 02-calc.js 3+4
//3+4=7

if(typeof(arg)==='undefined'||
    arg==='--help'||
    arg==='-h'){
  help();
}else{
  calc();
}

function help(){
  const msg = ''
    + 'usage: cmd-name[OPTION][expression]\n'
    + 'evaluate the expression.\n'
    + '\n'
    + 'Mandatory arguments to long options are mandatory for short options too.\n'
    + '-h,--help output help information and exit\n';

  log(msg);
}

function calc(){
  try{
    log(`${arg}=${eval(arg)}`);
  }catch(e){
    console.error(`${arg}不是合法的数学表达式！`);
  }
}

